package com.kaizzi.data.jdl.helper;

import com.jfinal.plugin.activerecord.Page;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.TrendUtil;
import com.kaizzi.data.jdl.been.TrendBeen;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.model.Page35Model;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.Trend;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @author Kzen
 * @Description description
 * @date 2020/10/19
 */
@Component
@Slf4j
public class PageData35 {

    public String getData(ExcelModel model){
        List<Page35Model> page35s = model.getPage35s();
        System.out.println(JsonUtil.serialize(page35s));

        Page35 page35 = new Page35();
        List<Page35Model> lastModel = page35s.stream().skip(0).limit(3).collect(toList());
        List<Page35Model> thisModel = page35s.stream().skip(4).limit(3).collect(toList());
        List<Page35Model> statisticsModel = page35s.stream().skip(8).collect(toList());

//        System.out.println("last:"+JsonUtil.serialize(lastModel));
//        System.out.println("this:"+JsonUtil.serialize(thisModel));
//        System.out.println("statistic:"+JsonUtil.serialize(statisticsModel));
//
//        List<TempPage35> pvs = getTempData(lastModel,thisModel,0);
//        List<TempPage35> uvs = getTempData(lastModel,thisModel,1);
//        List<TempPage35> avgs = getTempData(lastModel,thisModel,2);
//
//        TrendUtil.trendSetting(pvs);
//        TrendUtil.trendSetting(uvs);
//        TrendUtil.trendSetting(avgs);

        page35.setTitle(LogicUtil.getTitle(model,35));
        page35.setDataPeriod(DateUtil.getLastWeekDate());
        page35.setPvs(getTempData(lastModel,thisModel,0));
        page35.setUvs(getTempData(lastModel,thisModel,1));
        page35.setAvgs(getTempData(lastModel,thisModel,2));
        page35.setPageStatistics(getStatistics(statisticsModel));

        return JsonUtil.serialize(page35);
    }

    private List<String> getStatistics(List<Page35Model> page35Models) {
        List<String> list = new ArrayList<>();
        for (Page35Model pageModel : page35Models) {
//            String nameValue = pageModel.getNameValue();
//            String index = pageModel.getIndex();
//            String clockIn = pageModel.getClockIn();
//            String teamSquare = pageModel.getTeamSquare();
            String tempStr = pageModel.getNameValue() + "：访问次数：" + pageModel.getIndex() + "；访问人数："
                    + pageModel.getClockIn() + "；点赞数：" + pageModel.getTeamSquare();
            list.add(tempStr);
        }
        return list;
    }

    private List<TempPage35> getTempData(List<Page35Model> lastModel,List<Page35Model> thisModel,int sort) {
        List<TempPage35> list = new ArrayList<>();
        List<String> dateList = DateUtil.getDateList(2);

        TempPage35 tempPage1 = getTempPage(lastModel, dateList.get(0), sort);
        TempPage35 tempPage2 = getTempPage(thisModel, dateList.get(1), sort);
        list.add(tempPage1);
        list.add(tempPage2);

        TrendUtil.trendSetting(list);
        return list;
    }

    private TempPage35 getTempPage(List<Page35Model> models,String date,int sort){
        TempPage35 tempData = new TempPage35();
        tempData.setDate(new TrendBeen(date,0));
        tempData.setIndex(new TrendBeen(getFormatParam(models.get(sort).getIndex()),0));
        tempData.setClockIn(new TrendBeen(getFormatParam(models.get(sort).getClockIn()),0));
        tempData.setTeamSquare(new TrendBeen(getFormatParam(models.get(sort).getTeamSquare()),0));
        tempData.setPointMarket(new TrendBeen(getFormatParam(models.get(sort).getPointMarket()),0));
        tempData.setCashMarket(new TrendBeen(getFormatParam(models.get(sort).getCashMarket()),0));
        tempData.setClub(new TrendBeen(getFormatParam(models.get(sort).getClub()),0));
        tempData.setPointStep(new TrendBeen(getFormatParam(models.get(sort).getPointStep()),0));
        tempData.setPointLog(new TrendBeen(getFormatParam(models.get(sort).getPointLog()),0));

        return tempData;
    }

    private String getFormatParam(String string){
        return string.replace(",","");
    }


    @Data
    private static class Page35{
        private String title;
        private String dataPeriod;
        private List<TempPage35> pvs;
        private List<TempPage35> uvs;
        private List<TempPage35> avgs;
        private List<String> pageStatistics;
    }

    @Data
    private static class TempPage35{
        private TrendBeen date;
        private TrendBeen index;
        private TrendBeen clockIn;
        private TrendBeen teamSquare;
        private TrendBeen pointMarket;
        private TrendBeen cashMarket;
        private TrendBeen club;
        private TrendBeen pointStep;
        private TrendBeen pointLog;
    }
}
